package com.homedo.odin.solution.service.common.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.homedo.core.common.util.HttpClient;
import com.homedo.odin.solution.config.PlatformConfigHelper;
import com.homedo.odin.solution.model.dto.PageBean;
import com.homedo.odin.solution.model.dto.search.SearchResponseBean;
import com.homedo.odin.solution.model.dto.search.SearchSolution;
import com.homedo.odin.solution.service.common.SearchService;
import java.util.HashMap;
import java.util.List;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author: zhaoxudong
 * @date: 2018/4/8 11:37
 * @Description:
 */
@Service
public class SearchServiceImpl implements SearchService {

    @Autowired
    PlatformConfigHelper platformConfigHelper;

    @Override
    public SearchResponseBean<SearchSolution> querySolutionFromSearch(String solutionType, String keyword,
            PageBean pageBean) {
        HashMap<String, Object> paramsMap = new HashMap<>();
        //资源注册key
        paramsMap.put("source", "all81keyword");
        //返回数据类型
        paramsMap.put("responseformat", "standardjson");
        paramsMap.put("limit", "10000");
        //分页信息
        paramsMap.put("page", pageBean.getCurrentPage());
        //分页信息
        paramsMap.put("pageSize", pageBean.getPageSize());
        //方案类型
        if (StringUtils.isNoneBlank(solutionType)) {
            paramsMap.put("solutionType", solutionType);
        }
        paramsMap.put("keyword", keyword);

        String responseJson = HttpClient.get(platformConfigHelper.getSearchUrl(), paramsMap);
        List<SearchResponseBean> result = JSON.parseObject(responseJson, List.class);
        if (CollectionUtils.isEmpty(result)) {
            return null;
        } else {
            return JSON.parseObject(JSON.toJSONString(result.get(0)),
                    new TypeReference<SearchResponseBean<SearchSolution>>() {
                    });
        }
    }
}
